/*
 * Copyright (C) 2011-2016 Markus Junginger, greenrobot (http://greenrobot.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.greenrobot.greendao.sample;

import ohos.app.Context;
import ohos.data.rdb.RdbStore;
import ohos.data.resultset.ResultSetHook;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import org.greenrobot.greendao.AbstractDaoMaster;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseOpenHelper;
import org.greenrobot.greendao.database.StandardDatabase;
import org.greenrobot.greendao.identityscope.IdentityScopeType;

/**
 * Master of DAO (schema version 1000): knows all DAOs.
 */
public class DaoMaster extends AbstractDaoMaster {
    /**
     * Module.
     */
    public static final int DAO_SAMPLE = 0x00202;

    /**
     * Schema version.
     */
    public static final int SCHEMA_VERSION = 1000;

    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, DAO_SAMPLE, "greenDAO sample app");

    /**
     * DaoMaster.
     *
     * @param db greenDao database
     */
    public DaoMaster(Database db) {
        super(db, SCHEMA_VERSION);
        registerDaoClass(PersonDao.class);
    }

    /**
     * DaoMaster.
     *
     * @param db RdbStore
     */
    public DaoMaster(RdbStore db) {
        this(new StandardDatabase(db));
    }

    /**
     * Creates underlying database table using DAOs.
     *
     * @param db database
     * @param isExists table present or not
     */
    public static void createAllTables(Database db, boolean isExists) {
        PersonDao.createTable(db, isExists);
    }

    /**
     * Drops underlying database table using DAOs.
     *
     * @param db database
     * @param isExists table present
     */
    public static void dropAllTables(Database db, boolean isExists) {
        PersonDao.dropTable(db, isExists);
    }

    /**
     * WARNING: Drops all table on Upgrade! Use only during development.
     * Convenience method using DevOpenHelper.
     *
     * @param context context
     * @param name session name
     * @return session
     */
    public static DaoSession newDevSession(Context context, String name) {
        Database db = new DevOpenHelper(context, name).getWritableDb();
        DaoMaster daoMaster = new DaoMaster(db);
        return daoMaster.newSession();
    }

    @Override
    public DaoSession newSession() {
        return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
    }

    @Override
    public DaoSession newSession(IdentityScopeType type) {
        return new DaoSession(db, type, daoConfigMap);
    }

    /**
     * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} -
     *
     * @since 2020-08-25
     */
    public abstract static class OpenHelper extends DatabaseOpenHelper {
        /**
         * OpenHelper.
         *
         * @param context context
         * @param name name
         */
        public OpenHelper(Context context, String name) {
            super(context, name, DaoMaster.SCHEMA_VERSION);
        }

        /**
         * OpenHelper.
         *
         * @param context context
         * @param name name
         * @param factory ResultSetHook
         */
        public OpenHelper(Context context, String name, ResultSetHook factory) {
            super(context, name, factory, DaoMaster.SCHEMA_VERSION);
        }

        @Override
        public void onCreate(Database db) {
            HiLog.info(DaoMaster.TAG, "Creating tables for schema version " + DaoMaster.SCHEMA_VERSION);
            DaoMaster.createAllTables(db, false);
        }
    }

    /**
     * WARNING: Drops all table on Upgrade! Use only during development.
     *
     * @since 2020-08-25
     */
    public static class DevOpenHelper extends OpenHelper {
        /**
         * DevOpenHelper.
         *
         * @param context context
         * @param name name
         */
        public DevOpenHelper(Context context, String name) {
            super(context, name);
        }

        /**
         * DevOpenHelper.
         *
         * @param context context
         * @param name name
         * @param factory ResultSetHook
         */
        public DevOpenHelper(Context context, String name, ResultSetHook factory) {
            super(context, name, factory);
        }

        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            HiLog.info(DaoMaster.TAG,
                "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
            DaoMaster.dropAllTables(db, true);
            onCreate(db);
        }
    }
}
